package com.panghu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panghu.entity.ApiResult;
import com.panghu.entity.ExamManage;
import com.panghu.mapper.ExamManageMapper;
import com.panghu.service.ExamManageService;
import com.panghu.service.impl.ExamManageServiceImpl;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 考试管理表 前端控制器
 * </p>
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController
public class ExamManageController {
    //exam的mapper是自定义的 查询所有的判断 选择 填空
    @Autowired
    private ExamManageService examManageService;


    @GetMapping("/exams")
    public ApiResult findAll(){
        //查询所有的考试场次和试卷
        System.out.println("不分页查询所有的考试场次和试卷");
        ApiResult api = ApiResultHandler.buildApiResult(200,"查询成功",examManageService.list());
        return api;
    }

    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        //分页查询所有的考试场次和试卷
        ApiResult<Object> api = new ApiResult<>();

        Page<ExamManage> p = new Page(page,size);
        //默认情况下 查询该表的所有数据
        IPage<ExamManage> iPage = examManageService.page(p,null);
        return ApiResultHandler.success(iPage);

    }

    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable("examCode") Integer examCode){
        //按考试id进行查询
        ExamManage exam = examManageService.getById(examCode);
        if(exam == null){
            return ApiResultHandler.buildApiResult(1000,"考试编号不存在",null);
        }
        return ApiResultHandler.buildApiResult(200,"查询成功",exam);
    }

    @DeleteMapping("/exam/{examCode}")
    public ApiResult deleteById(@PathVariable("examCode") Integer examCode){
        //按考试id进行删除
        boolean isTrue = examManageService.removeById(examCode);
        if(!isTrue){
            return ApiResultHandler.buildApiResult(10000,"考试不存在",null);
        }
        return ApiResultHandler.success();
    }

    @PutMapping("/exam")
    public ApiResult update(@RequestBody ExamManage exammanage){
        //更新考试信息
        boolean ifTrue = examManageService.saveOrUpdate(exammanage);
        if(!true){
            return ApiResultHandler.buildApiResult(20000,"请求失败",ifTrue);
        }else {
            return ApiResultHandler.buildApiResult(200,"更新成功",ifTrue);
        }
    }

    @PostMapping("/exam")
    public ApiResult add(@RequestBody ExamManage exammanage){
        //增加考试信息
        boolean res = examManageService.save(exammanage);
        if (res) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
    }

    @GetMapping("/examManagePaperId")
    public ApiResult findOnlyPaperId() {
        //查询最后一场考试的id 传递给前端用于自增
        ExamManage exam = examManageService.findOnlyPaperId();
        return ApiResultHandler.success(exam);
    }
}

